package com.brand.prisd.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.brand.prisd.R;
import com.orhanobut.logger.Logger;
import com.tuya.smart.android.user.api.ILoginCallback;
import com.tuya.smart.android.user.bean.User;
import com.tuya.smart.home.sdk.TuyaHomeSdk;

/**
 * 输入验证码页面
 */
public class InputCodeActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_code;
    private String phoneNum;
    private Button btn_confirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input_code);
        Intent intent = getIntent();
        phoneNum = intent.getStringExtra("phoneNum");
        initView();
    }

    private void initView() {
        et_code = findViewById(R.id.et_code);
        btn_confirm = findViewById(R.id.btn_confirm);
        btn_confirm.setOnClickListener(this);

    }

    private void go2MainActivity(){
        startActivity(new Intent(this,MainActivity.class));
        finish();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_confirm:
                login();
                break;
        }
    }
    private void login(){
        String code = et_code.getText().toString();
        //手机验证码登陆
        TuyaHomeSdk.getUserInstance().loginWithPhone("86", phoneNum, code, new ILoginCallback() {
            @Override
            public void onSuccess(User user) {
                Toast.makeText(getApplicationContext(), "登录成功，用户名：" + TuyaHomeSdk.getUserInstance().getUser().getUsername(), Toast.LENGTH_SHORT).show();
                go2MainActivity();
            }

            @Override
            public void onError(String code, String error) {
                Toast.makeText(getApplicationContext(), error, Toast.LENGTH_SHORT).show();
                Logger.e("code: "+code+" "+"error: "+error);
            }
        });
    }
}
